package ch.ti.kmltomap;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import com.google.android.gms.maps.GoogleMap;

public class Placemark extends Feature{

	public List<Geometry> geometries = new ArrayList<Geometry>();
	
	public Placemark(XmlPullParser xpp){
		try{
			this.id = xpp.getAttributeValue(null, "id");
			int eventType = xpp.next();
			String tagName = "";
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if(eventType == XmlPullParser.START_TAG){
					if(xpp.getName().equals("Point")){
						this.geometries.add(new Point(xpp));
					}else if(xpp.getName().equals("LineString")){
						this.geometries.add(new LineString(xpp));
					}else if(xpp.getName().equals("LinearRing")){
						this.geometries.add(new LinearRing(xpp));
					}else if(xpp.getName().equals("Polygon")){
						this.geometries.add(new Polygon(xpp));
					}
					tagName = xpp.getName();
					
				}else if(eventType == XmlPullParser.TEXT){
					if(tagName.equals("name")){
						this.name = this.name + xpp.getText().trim();
					}else if(tagName.equals("open")){
						this.open = xpp.getText().trim().equals("1");
					}else if(tagName.equals("Snippet")){
						this.snippet = this.snippet + xpp.getText().trim();
					}else if(tagName.equals("visibility")){
						this.visibility = xpp.getText().trim().equals("1");
					}else if(tagName.equals("address")){
						this.address = xpp.getText();
					}else if(tagName.equals("phoneNumber")){
						this.phoneNumber = this.phoneNumber + xpp.getText().trim();
					}else if(tagName.equals("description")){
						this.description = this.description + xpp.getText().trim();
					}
				}else if(eventType == XmlPullParser.END_TAG){
					if(xpp.getName().equals("Placemark")){
						break;
					}
				}
				eventType = xpp.next();
			}
		}catch (Exception e) {
		}
	}
	
	public void draw(GoogleMap map){
		for (Geometry geometry : this.geometries) {
			geometry.draw(map, this.snippet);
		}
		
	}
	
}
